package com.tianmu.demo.ad;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

import com.tianmu.ad.BannerAd;
import com.tianmu.ad.bean.BannerAdInfo;
import com.tianmu.ad.error.TianmuError;
import com.tianmu.ad.listener.BannerAdListener;
import com.tianmu.utils.TianmuLogUtil;
import com.tianmudemo.R;
import com.tianmu.demo.constant.TianmuDemoConstant;

/**
 * @Description:
 * @Author: maipian
 * @CreateDate: 2021/9/9 6:02 PM
 */
public class BannerAdActivity extends AppCompatActivity {

    private FrameLayout flContainer;
    private BannerAd bannerAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);

        flContainer = findViewById(R.id.flContainer);

        bannerAd = new BannerAd(this, flContainer);
        bannerAd.setListener(new BannerAdListener() {
            @Override
            public void onAdExpose(BannerAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG, "onAdExpose");
            }

            @Override
            public void onAdClick(BannerAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG, "onAdClick");
            }

            @Override
            public void onAdClose(BannerAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG, "onAdClose");
            }

            @Override
            public void onAdReceive(BannerAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG, "onAdReceive");
            }

            @Override
            public void onAdFailed(TianmuError tianmuError) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG, "onAdFailed error" + tianmuError.getError());
            }
        });
        bannerAd.loadAd(TianmuDemoConstant.BANNER_ID);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (bannerAd != null) {
            bannerAd.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (bannerAd != null) {
            bannerAd.resume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bannerAd != null) {
            bannerAd.release();
            bannerAd = null;
        }
    }
}
